<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="layui/css/layui.css" media="all" rel="stylesheet">
    <script src="layui/layui.js" type="text/javascript"></script>
    <script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
</head>
<body class="layui-layout-body">
<div class="layui-container" style="margin-top: 50px;width: 980px;">
    <table class="layui-table" id="test" lay-filter="test"></table>
    <script id="barDemo1" type="text/html">
        <a class="layui-btn layui-btn-xs" lay-event="addTime">延时</a>
    </script>
    <script id="barDemo2" type="text/html">
        <input type="checkbox" id="{{d.student.student_id}}" value="{{d.student_status}}" lay-skin="switch"
               lay-filter="status" lay-text="正常|禁用" {{ d.student_status== 0 ? 'checked' : '' }}>
    </script>
    <script id="barDemo3" type="text/html">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">作废</a>

    </script>
    <script charset="utf-8" src="layui/layui.js"></script>
    <script>
        layui.use(['table', 'transfer', 'layer', 'util', 'form', 'laydate'], function () {
            var table = layui.table;
            var transfer = layui.transfer;
            var layer = layui.layer;
            var util = layui.util;
            var form = layui.form;
            var laydate = layui.laydate;
            form.on('switch(status)', function (obj) {
                //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
                var status = 0;
                if (obj.elem.checked) {
                    //此时指解禁
                    status = 0;
                } else {
                    //此时指禁用
                    status = 1;
                }
                var stu_id = obj.elem.id;
                $.ajax({
                    url: '/question/test/editStudentStatus/' + sessionStorage.getItem("testId") + '/' + stu_id + '/' + status, //请求url
                    data: '', //请求参数，类似于username=xxx&password=xxx
                    type: 'put', //请求方式
                    dataType: 'json', //返回值的类型
                    contentType: 'application/json;charset=utf-8',
                    success: function (result) {  //服务器成功返回时执行的函数
                        console.log(JSON.stringify(result));
                        if (result.code == 2000) {
                            alert("修改成功！");

                        } else {
                            alert("修改失败!\n" + result.msg);
                        }
                    },
                    error: function () {
                        alert("修改失败，网络异常！");
                    }
                });
                window.location.href = "test_stu_manage.html";
            });
            table.render({
                elem: '#test'
                , url: '/question/test/getTestStudent/' + sessionStorage.getItem("testId")
                , title: '考生列表'
                , cols: [
                    [
                        {
                            field: 'student_id', width: 80, title: '编号', sort: true, templet: function (data) {
                                return data.student.student_id;
                            }
                        }
                        , {
                        field: 'student_name', width: 100, title: '学生姓名', sort: true, templet: function (data) {
                            return data.student.student_name;
                        }
                    }
                        , {
                        field: 'class_name', width: 110, title: '班级', templet: function (data) {
                            return data.student.classes.class_name;
                        }
                    }
                        , {
                        field: 'login_ip', width: 110, title: 'IP地址'
                    }
                        , {
                        field: 'student_end_time', width: 160, title: '延时', templet: function (data) {
                            if (data.is_add_time == 0) {
                                return '未延时';
                            } else {
                                return data.student_end_time;
                            }
                        }
                    }
                        , {
                        field: 'is_cancel', width: 90, title: '试卷状态', templet: function (data) {
                            if (data.is_cancel == 0) {
                                return '正常';
                            } else if (data.is_cancel == 1) {
                                return '作废';
                            }
                        }
                    }
                        , {fixed: 'right', title: '延时', toolbar: '#barDemo1', width: 90}
                        , {fixed: 'right', title: '禁用/解禁', width: 120, templet: '#barDemo2'}
                        , {fixed: 'right', title: '作废', toolbar: '#barDemo3', width: 90}
                    ]
                ]
                , page: false
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                var oldTime = data.student_end_time;
                var stu_id = data.student.student_id;
                if (obj.event === 'addTime') {
                    var time;
                    var index = layer.open({
                        type: 1,
                        title: '延时',
                        area: ['350px', '200px'],
                        skin: 'layer-extskin-overauto',
                        content: $('#popUpdate').html(),
                        success: function () {
                            laydate.render({
                                elem: '#endTime',
                                trigger: 'click',
                                type: 'datetime'
                            });

                            $("#submit").click(function () {
                                time = $("#endTime").val();
                                if (time.length == 0) {
                                    alert("结束时间不得为空");
                                    return;
                                }

                                if (new Date(oldTime).getTime() - new Date(time).getTime() > 0) {
                                    alert("修改后时间不得早于之前的结束时间，请检查");
                                    return;
                                }
                                console.log("id:" + stu_id);
                                console.log(time);
                                $.ajax({
                                    url: '/question/test/addTimeForStudent/' + sessionStorage.getItem("testId") + '/' + stu_id, //请求url
                                    data: JSON.stringify({"time": time}), //请求参数，类似于username=xxx&password=xxx
                                    type: 'put', //请求方式
                                    dataType: 'json', //返回值的类型
                                    contentType: 'application/json;charset=utf-8',
                                    success: function (result) {  //服务器成功返回时执行的函数
                                        console.log(JSON.stringify(result));
                                        if (result.code == 2000) {
                                            alert("修改成功！");
                                            layer.close(index);
                                            window.location.href = "test_stu_manage.html";
                                        } else {
                                            alert("修改失败!\n" + result.msg);
                                            return;
                                        }
                                    },
                                    error: function () {
                                        alert("修改失败，网络异常！");
                                        return;
                                    }
                                });
                            });

                        }
                    });

                }
                if (obj.event === 'del') {
                    layer.confirm('作废后将无法取消，确定要作废该学生的试卷吗？', function (index) {
                        $.ajax({
                            url: '/question/test/cancelStudentTest/' + sessionStorage.getItem("testId") + '/' + data.student.student_id,
                            data: '', //请求参数，类似于username=xxx&password=xxx
                            type: 'delete', //请求方式
                            dataType: 'json', //返回值的类型
                            contentType: 'application/json;charset=utf-8',
                            success: function (result) {  //服务器成功返回时执行的函数
                                console.log(JSON.stringify(result));
                                if (result.code == 2000) {
                                    alert("试卷已作废！");
                                    layer.close(index);
                                    window.location.href = "test_stu_manage.html";
                                } else {
                                    alert("操作失败!\n" + result.msg);

                                }
                            },
                            error: function () {
                                alert("修改失败，网络异常！");

                            }
                        });
                    });
                }
            });
        });
    </script>

</div>
<script id="popUpdate" type="text/html">
    <div class="layui-form-mid">
        <label class="layui-form-label">结束时间：</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="endTime" id="endTime"
                   placeholder="yyyy-MM-dd HH:mm:ss">
        </div>
        <div class="layui-form-item">
            <button class="layui-btn" id="submit" style="margin-left: 160px;">确认</button>
        </div>
    </div>

</script>
<script>
    //二级菜单联动
    layui.use('element', function () {
        var element = layui.element;

    });
</script>
</body>
</html>